package BasicKnowledge.Basic.IO;

import java.io.*;


/**
 * @time: 2021/2/2/002 16:38
 * @author: Zhurunhe
 */
public class Demo8 {
    public static void main(String[] args) {
        String path="G:/abc.txt";
        String topath="G:/bcd.txt";
        //testInputStreamReader(path);
        testOutputStreamWriter(topath);

    }

    private static void testOutputStreamWriter(String topath) {
        OutputStreamWriter outputStreamWriter= null;
        try {
            FileOutputStream fileOutputStream=new FileOutputStream(topath,true);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream,"GBK");

            outputStreamWriter.write("使用转换流，可以避免中文乱码");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStreamWriter!=null){
                try {
                    outputStreamWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void testInputStreamReader(String path) {
        InputStreamReader inputStreamReader= null;
        try {
            FileInputStream fileInputStream=new FileInputStream(path);
            inputStreamReader = new InputStreamReader(fileInputStream,"GBK");

            int isr=inputStreamReader.read();
            while (isr!=-1){
                System.out.print((char) isr);
                isr=inputStreamReader.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (inputStreamReader!=null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


    }
}
